/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package character_13;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import java.time.Period;
import java.time.chrono.MinguoDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 *
 * @author Administrator
 */
public class Character_13 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws InterruptedException, ParseException {
        
        //Date用来当成时间轴上的某一点
        Date date1 = new Date(System.currentTimeMillis());
        System.out.println(date1.getTime());//getTime获取毫秒数
        System.out.println(date1);
        
        //Thread.sleep(1000);
        
        Date date2 = new Date();
        System.out.println(date2.getTime());
        
        System.out.println(System.currentTimeMillis());//获得系统时间
        
        //DateFormat格式化时间日期,字符串时间格式处理
        Date date = new Date();
        dateInstanceDemo(date);
        
        timeInstanceDemo(date);
        
        dateTimeInstanceDemo(date);
        
        DateFormat dateFormat1 = new SimpleDateFormat("EE-MM-dd-yy");
        System.out.println(dateFormat1.format(date));
        
        DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd-EE");
        System.out.println(dateFormat2.format(date));
        
        //将指定字符串剖析为Date实例
        DateFormat dateFormat3 = new SimpleDateFormat("yyyy-mm-dd");
        Date date3 = dateFormat3.parse("1991-06-20");
        Date date4 = new Date();
        long life = date4.getTime() - date3.getTime();
        System.out.println("周岁数" + life/(365 * 24 * 60 * 60 * 1000L));
        
        //处理时间日期的Calender
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH));//从0开始
        System.out.println(calendar.get(Calendar.DATE));
        
        calculateAge();
        
        //时区设定
        TimeZone timeZone = TimeZone.getDefault();
        System.out.println(timeZone.getDisplayName());
        System.out.println("时区ID:" + timeZone.getID());//获取时区ID
        System.out.println("日光节约时数:" + timeZone.getDSTSavings());
        System.out.println("偏移毫秒数:" + timeZone.getRawOffset());
        
        TimeZone taibei = TimeZone.getTimeZone("Asia/Taipei");
        TimeZone gebenhagen = TimeZone.getTimeZone("Europe/Copenhagen");
        
        Calendar cal = Calendar.getInstance(taibei);
        showTime(cal);
        
        cal.setTimeZone(gebenhagen);
        showTime(cal);
        
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(1975, Calendar.MARCH, 31, 23, 59, 59);
        System.out.println(calendar1.getTime().getTime());
        calendar1.add(Calendar.SECOND, 1);
        System.out.println(calendar1.getTime().getTime());
        
        for(Month month : Month.values()){
            System.out.println("original:" + month.ordinal() + "/" + month.getValue() + "/" + month);
        }
        
        //对时间的运算
        calendar.set(1975, Calendar.MAY, 26, 0, 0, 0);
        calendar.add(Calendar.DAY_OF_MONTH, 5);
        calendar.add(Calendar.MONTH, 6);
        calendar.add(Calendar.WEEK_OF_MONTH, 3);
        SimpleDateFormat df = new SimpleDateFormat("E MM/dd/yyyy");
        System.out.println(df.format(calendar.getTime()));
        
        //以上等效于JDK8里面的
        System.out.println(
                LocalDate.of(1975, 5, 26)
                        .plusDays(5)
                        .plusMonths(6)
                        .plusWeeks(3)
                        .format(DateTimeFormatter.ofPattern("E MM/dd/yyyy")));
        
        System.out.println(
                LocalDate.of(1975, 5, 26)
                        .plus(Period.ofDays(5))
                        .plus(Period.ofMonths(6))
                        .plus(Period.ofWeeks(3))
                        .format(DateTimeFormatter.ofPattern("E MM/dd/yyyy")));
        
        LocalDate birth = LocalDate.parse("1991-06-20");
        LocalDate now = LocalDate.now();
        Period period = Period.between(birth, now);
        System.out.printf("你活了 %d 年 %d 月 %d 日 \n",
                period.getYears(), period.getMonths(), period.getDays());
        
        LocalDate birth1 = LocalDate.of(1991, 6, 20);
        System.out.println("岁数：" + ChronoUnit.YEARS.between(birth1, now));
        System.out.println("天数：" + ChronoUnit.DAYS.between(birth1, now));
        
        
        //民国日期
        System.out.println(MinguoDate.of(64, 5, 1).atTime(LocalTime.of(3, 30, 0)));
    }

    private static void dateInstanceDemo(Date date) {
        System.out.println(DateFormat.getDateInstance(DateFormat.LONG).format(date));//2017年2月17日
        System.out.println(DateFormat.getDateInstance(DateFormat.SHORT).format(date));//17-2-17
    }

    private static void timeInstanceDemo(Date date) {
        System.out.println(DateFormat.getTimeInstance(DateFormat.LONG).format(date));//下午10时19分22秒
        System.out.println(DateFormat.getTimeInstance(DateFormat.MEDIUM).format(date));//22:19:22
        System.out.println(DateFormat.getTimeInstance(DateFormat.SHORT).format(date));//下午10:19
    }

    private static void dateTimeInstanceDemo(Date date) {
        System.out.println(DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(date));//2017年2月17日 下午10时22分13秒
        System.out.println(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(date));//17-2-17 22:22:13
        System.out.println(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date));//17-2-17 下午10:22
    }

    private static void calculateAge() {
        Calendar birth = Calendar.getInstance();
        birth.set(1991, Calendar.JUNE, 20);
        Calendar now = Calendar.getInstance();
        System.out.println("岁数：" + yearsBetweent(birth, now));
        System.out.println("天数：" + daysBetweent(birth, now));
    }

    private static long yearsBetweent(Calendar begin, Calendar end) {
        Calendar calendar = (Calendar) begin.clone();
        long years = 0;
        while (calendar.before(end)) {            
            calendar.add(Calendar.YEAR, 1);
            years ++;
        }
        return years - 1;
    }

    private static long daysBetweent(Calendar begin, Calendar end) {
        Calendar calendar = (Calendar) begin.clone();
        long days = 0;
        while (calendar.before(end)) {            
            calendar.add(Calendar.DATE, 1);
            days ++;
        }
        return days - 1;
    }

    private static void showTime(Calendar calTaiBei) {
        System.out.println(calTaiBei.getTimeZone().getDisplayName());
        System.out.println(calTaiBei.get(Calendar.HOUR) + ":" + calTaiBei.get(Calendar.MINUTE));
    }
    
    
    
}
